﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core.Extensions
{
    public static class AESExtensions
    {
        /// <summary>
        /// 将UTF8字符串明文加密成base64密文
        /// </summary>
        /// <param name="plaintext">字符串明文</param>
        /// <param name="key">字符串密钥</param>
        /// <param name="iv">字符串向量</param>
        /// <returns></returns>
        public static string AesEncrypt(this string plaintext,string key,string? iv=null)
        {
            byte[]? _iv=null;
            if (!string.IsNullOrEmpty(iv))
            {
                _iv = Encoding.UTF8.GetBytes(iv);
            }
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key),_iv))
            {
                return Convert.ToBase64String(aes.Encrypt(plaintext));
            }
        }

        /// <summary>
        /// 将UTF8字符串明文加密成byte[]密文
        /// </summary>
        /// <param name="plaintext">字符串明文</param>
        /// <param name="key">字符串密钥</param>
        /// <param name="iv">字符串向量</param>
        /// <returns></returns>
        public static byte[] AesEncryptForByteArray(this string plaintext, string key, string? iv = null)
        {
            byte[]? _iv = null;
            if (!string.IsNullOrEmpty(iv))
            {
                _iv = Encoding.UTF8.GetBytes(iv);
            }
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key), _iv))
            {
                return aes.Encrypt(plaintext);
            }
        }

        /// <summary>
        /// 将字符串密文解密成UTF8明文
        /// </summary>
        /// <param name="plaintext">字符串明文</param>
        /// <param name="key">字符串密钥</param>
        /// <param name="iv">字符串向量</param>
        /// <returns></returns>
        public static string AesDecrypt(this string ciphertext, string key, string? iv = null)
        {
            byte[]? _iv = null;
            if (!string.IsNullOrEmpty(iv))
            {
                _iv = Encoding.UTF8.GetBytes(iv);
            }
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key), _iv))
            {
                return aes.Decrypt(ciphertext);
            }
        }

        #region [byte[]对像AES加解密]
        public static byte[] ToAesEncryptByteArray(this byte[] bytes, string key)
        {
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key)))
            {
                return aes.Encrypt(bytes);
            }
        }
        public static string ToAesEncryptString(this byte[] bytes, string key)
        {
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key)))
            {
                return Encoding.UTF8.GetString(aes.Encrypt(bytes));
            }
        }
        public static string ToAesDecryptString(this byte[] bytes, string key)
        {
            using (Encryption.AES aes = new Encryption.AES(Encoding.UTF8.GetBytes(key)))
            {
                return aes.Decrypt(bytes);
            }
        }
        #endregion

    }
}
